<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-table-1080 flex-box" :style="getStyle('head')">
      <div class="table-inside">
        <div class="header-area table-item">
          <div class="item-info">
            <template v-for="(item) in (optionsInfo['colOptions'])">
              <div class="item" :class="item.className" :style="setFlex(item,'head')">
                {{ item.label }}
              </div>
            </template>
          </div>
        </div>
        <div class="content-area table-item" :key="uuid">
          <div class="content-area-inside">
          <vue-seamless-scroll
              class="scroll"
              :data="drugPriceListInfo"
              :hoverStop="false"
              @ScrollEnd="handleScrollEnd"
              :class-option="optionDrugList()"
              ref="drug-scroll"
          >
              <template v-for="(item) in drugPriceListInfo">
                <div class="item-info">
                  <template v-if="true" v-for="(items) in optionsInfo['colOptions']">
                    <div class="item" :class="items.className" :style="setFlex(items,'list')">
                      <ScrollX>
                        {{ item[items['value']] }}
                      </ScrollX>
                    </div>
                  </template>
                </div>
              </template>
          </vue-seamless-scroll>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {NoticeBar} from 'vant';
import {v4 as uuid} from "uuid";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import {pxToVw, remToPx,hasOverLayer} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from 'vue-seamless-scroll';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import NoticeBarInfo from "@c/public/noticeBar/index.vue";
import {bizFields, containers, customFields, sceneFields} from "@c/public/form-designer/widgetsConfig";

export default {
  name: "scene-public-table-1080-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    NoticeBarInfo,
    CompOperate,
    ScrollX,
    NoticeBar,
    vueSeamlessScroll
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      uuid: '',
      optionsInfo:Object(),
    }
  },

  computed: {

    // 模板的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.height));
            return Obj;
        }
      };
    },

    // 设置宽度和字号
    setFlex() {
      return function (item, val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "flex", (item.flex) || 1);
            return Obj;
          case "list":
            Reflect.set(Obj, "flex", (item.flex) || 1);
            return Obj;
        }
      }
    },

    // 药品公示向上的滚动
    optionDrugList() {
      return function () {
        let {scrollNumber, scrollState} = (this.optionsInfo);
        let info = scrollState? scrollNumber : 99999;
        return {
          step: [1, 1.5, 2, 2.5, 3][0], // 数值越大速度滚动越快
          hoverStop: true,
          limitMoveNum: info, // 开始无缝滚动的数据量 this.dataList.length
          direction: 1 // 0向下 1向上 2向左 3向右
        };
      }
    },

    //药品价格公示
    drugPriceListInfo() {
      let devType = (this.devType);
      let drugPriceInfo = this.$store.getters['getDrugPriceListInfo'];
      if (devType) {
        return drugPriceInfo['apiData']
      } else {
        return drugPriceInfo['mockData']
      }
    },

    // 滚动的设置项目
    getScrollOptions() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let speedInfo = (info?.speed) > 10 ? 10 : (info.speed);
        console.log(speedInfo)
        let speedRem = (parseInt(remToPx(speedInfo)))
        // console.log(speedRem)
        console.log(val);
        return {
          text: `${val}`,                             // 通知内容
          fontSize: (info.listFontSize),             // 通知内容文字大小（默认16px）
          fontWeight: (info.listFontWeight),
          color: (info.listFontColor),               // 通知内容文字颜色(默认#f60)
          delay: '1000',                             // 动画延迟时间(默认一秒后开始滚动，单位毫秒)
          speed: 60,                         // 滚动速率默认50 (px/s)
          scrollable: true,
          position: 'left'
        }
      }

    },
  },

  mounted() {
    this.handleOnMounted();
    // let drugPriceInfo = this.$store.getters['getDrugPriceListInfo'];
    // console.log(drugPriceInfo['mockData'])
    // this.executeMethod('drugPriceListInfo',drugPriceInfo['mockData'].slice(0,10))
  },

  methods: {
    handleScrollEnd() {

    },

    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "publishListInfo":
        case "drugPriceListInfo":
          console.log(data)
          let publicListInfo = this.$lodash.cloneDeep(data);
          await this.$store.dispatch('setDrugPriceListInfo', publicListInfo);
          break
      }
    },
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        // let comps = this.$lodash.concat(containers, bizFields, customFields, sceneFields);
        // let type = (val.type);
        // const originalComp = this.$lodash.find(comps, {type: type});
        // let originalKey = originalComp['options']['config'];
        // let valKey = val['options']['config'];
        // let originalOptions = originalComp['options'][originalKey];
        // let valOptions = val['options'][valKey];
        // this.optionsInfo = {...originalOptions, ...valOptions};
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    },

    'optionsInfo.scrollNumber': {
      handler: function (val, pre) {
        if (val && val !== pre) {
          this.uuid = uuid()
        }
      },
      deep: true,
      immediate: true
    },

    'optionsInfo.scrollState': {
      handler: function (val, pre) {
        if (val !== pre) {
          this.uuid = uuid()
        }
      },
      deep: true,
      immediate: true
    },

  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-table-1080 {
  background-color: transparent;
  color: #333;
  height: 100%;
  line-height: 100%;
  padding: 28px 30px;
  flex-direction: column;

  .table-inside {
    display: flex;
    height: 100%;
    flex-direction: column;
    background: snow;
    border-radius: 12px;
    overflow: hidden;

    //共有样式修改
    .table-item {
      display: flex;
      flex-direction: column;

      .item-info {
        width: 100%;
        display: flex;
        color: #333333;
        @include fontWeight();
        flex-direction: row;

        .item {
          flex: 1;
          font-size: 32px;
          text-align: left;
          line-height: 84px;
          padding: 0 10px;
          overflow: hidden;

          .item-inside {
            background: red;
          }
        }

        .medname {
          flex: 3.5;
        }

        .medfactory {
          flex: 2;
        }
      }
    }

    //头部样式
    .header-area {
      background: rgba(0, 89, 179, 1);

      .item-info {
        color: white;
      }
    }

    //具体内容页样式
    .content-area {
      flex-grow: 1;
      overflow: hidden;
      //height: calc(100% - 78px);
      .scroll {
        height: 100%;
        overflow: hidden;
      }

      .item-info {
        border: 4px rgba(229, 234, 239, 1) solid;
      }

      .item {
        text-align: left !important;

        :deep(.van-notice-bar) {
          @include fontWeight();
          background: transparent;
          height: 100%;
          width: 100%;
          align-items: flex-start;

          .van-notice-bar__wrap {
            height: 100%;
            margin: 0 auto;
            display: flex;
            min-height: 120px;
            justify-content: left;
            overflow: hidden;
            width: 100%;
            text-align: left;

            > div {
              font-size: 38px;
              font-weight: normal;
              line-height: 80px;
              color: #333333;
              text-align: left;
            }
          }
        }
      }

      .medname, .medunit {
        flex: 2;
        background: rgba(209, 217, 231, 1);
      }
    }
  }
}
</style>